home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / machine / mappy.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  14KB  |  586 lines

  1. /***************************************************************************
  2.  
  3.   machine.c
  4.  
  5.   Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  6.   I/O ports)
  7.  
  8. ***************************************************************************/
  9.  
  10. #include "driver.h"
  11. #include "cpu/m6809/m6809.h"
  12.  
  13.  
  14. unsigned char *mappy_sharedram;
  15. unsigned char *mappy_customio_1,*mappy_customio_2;
  16.  
  17. static unsigned char interrupt_enable_1,interrupt_enable_2;
  18. static int credits, coin, start1, start2;
  19. static int io_chip_1_enabled, io_chip_2_enabled;
  20.  
  21. void mappy_init_machine(void)
  22. {
  23.     /* Reset all flags */
  24.     credits = coin = start1 = start2 = 0;
  25.     interrupt_enable_1 = interrupt_enable_2 = 0;
  26. }
  27.  
  28. void motos_init_machine(void)
  29. {
  30.     /* Reset all flags */
  31.     credits = coin = start1 = start2 = 0;
  32. }
  33.  
  34.  
  35. READ_HANDLER( mappy_sharedram_r )
  36. {
  37.     return mappy_sharedram[offset];
  38. }
  39.  
  40. WRITE_HANDLER( mappy_sharedram_w )
  41. {
  42.     mappy_sharedram[offset] = data;
  43. }
  44.  
  45.  
  46. WRITE_HANDLER( mappy_customio_1_w )
  47. {
  48.     mappy_customio_1[offset] = data;
  49. }
  50.  
  51.  
  52. WRITE_HANDLER( mappy_customio_2_w )
  53. {
  54.     mappy_customio_2[offset] = data;
  55. }
  56.  
  57. WRITE_HANDLER( mappy_reset_2_w )
  58. {
  59.     io_chip_1_enabled = io_chip_2_enabled = 0;
  60.     cpu_set_reset_line(1,PULSE_LINE);
  61. }
  62.  
  63. WRITE_HANDLER( mappy_io_chips_enable_w )
  64. {
  65.     io_chip_1_enabled = io_chip_2_enabled = 1;
  66. }
  67.  
  68. /*************************************************************************************
  69.  *
  70.  *         Mappy custom I/O ports
  71.  *
  72.  *************************************************************************************/
  73.  
  74. READ_HANDLER( mappy_customio_1_r )
  75. {
  76.     static int crednum[] = { 1, 2, 3, 6, 1, 3, 1, 2 };
  77.     static int credden[] = { 1, 1, 1, 1, 2, 2, 3, 3 };
  78.     int val, temp, mode = mappy_customio_1[8];
  79.  
  80.     logerror("I/O read 1: mode %d offset %d\n", mode, offset);
  81.  
  82.     /* mode 3 is the standard, and returns actual important values */
  83.     if (mode == 1 || mode == 3)
  84.     {
  85.         switch (offset)
  86.         {
  87.             case 0:        /* Coin slots, low nibble of port 4 */
  88.             {
  89.                 static int lastval;
  90.  
  91.                 val = readinputport (4) & 0x0f;
  92.  
  93.                 /* bit 0 is a trigger for the coin slot */
  94.                 if ((val & 1) && ((val ^ lastval) & 1)) ++credits;
  95.  
  96.                 return lastval = val;
  97.             }
  98.  
  99.             case 1:        /* Start buttons, high nibble of port 4 */
  100.             {
  101.                 static int lastval;
  102.  
  103.                 temp = readinputport (1) & 7;
  104.                 val = readinputport (4) >> 4;
  105.  
  106.                 /* bit 0 is a trigger for the 1 player start */
  107.                 if ((val & 1) && ((val ^ lastval) & 1))
  108.                 {
  109.                     if (credits >= credden[temp]) credits -= credden[temp];
  110.                     else val &= ~1;    /* otherwise you can start with no credits! */
  111.                 }
  112.                 /* bit 1 is a trigger for the 2 player start */
  113.                 if ((val & 2) && ((val ^ lastval) & 2))
  114.                 {
  115.                     if (credits >= 2 * credden[temp]) credits -= 2 * credden[temp];
  116.                     else val &= ~2;    /* otherwise you can start with no credits! */
  117.                 }
  118.  
  119.                 return lastval = val;
  120.             }
  121.  
  122.             case 2:        /* High BCD of credits */
  123.                 temp = readinputport (1) & 7;
  124.                 return (credits * crednum[temp] / credden[temp]) / 10;
  125.  
  126.             case 3:        /* Low BCD of credits */
  127.                 temp = readinputport (1) & 7;
  128.                 return (credits * crednum[temp] / credden[temp]) % 10;
  129.  
  130.             case 4:        /* Player 1 joystick */
  131.                 return readinputport (3) & 0x0f;
  132.  
  133.             case 5:        /* Player 1 buttons */
  134.                 return readinputport (3) >> 4;
  135.  
  136.             case 6:        /* Player 2 joystick */
  137.                 return readinputport (5) & 0x0f;
  138.  
  139.             case 7:        /* Player 2 joystick */
  140.                 return readinputport (5) >> 4;
  141.         }
  142.     }
  143.  
  144.     /* mode 5 values are actually checked against these numbers during power up */
  145.     else if (mode == 5)
  146.     {
  147.         static int testvals[] = { 8, 4, 6, 14, 13, 9, 13 };
  148.         if (offset >= 1 && offset <= 7)
  149.             return testvals[offset - 1];
  150.     }
  151.  
  152.     /* by default, return what was stored there */
  153.     return mappy_customio_1[offset];
  154. }
  155.  
  156.  
  157. READ_HANDLER( mappy_customio_2_r )
  158. {
  159.     int mode = mappy_customio_2[8];
  160.  
  161.     logerror("I/O read 2: mode %d, offset %d\n", mappy_customio_2[8], offset);
  162.  
  163.     /* mode 4 is the standard, and returns actual important values */
  164.     if (mode == 4)
  165.     {
  166.         switch (offset)
  167.         {
  168.             case 0:        /* DSW1, low nibble */
  169.                 return readinputport (1) & 0x0f;
  170.  
  171.             case 1:        /* DSW1, high nibble */
  172.                 return readinputport (1) >> 4;
  173.  
  174.             case 2:        /* DSW0, low nibble */
  175.                 return readinputport (0) & 0x0f;
  176.  
  177.             case 4:        /* DSW0, high nibble */
  178.                 return readinputport (0) >> 4;
  179.  
  180.             case 6:        /* DSW2 - service switch */
  181.                 return readinputport (2) & 0x0f;
  182.  
  183.             case 3:        /* read, but unknown */
  184.             case 5:        /* read, but unknown */
  185.             case 7:        /* read, but unknown */
  186.                 return 0;
  187.         }
  188.     }
  189.  
  190.     /* mode 5 values are actually checked against these numbers during power up */
  191.     else if (mode == 5)
  192.     {
  193.         static int testvals[] = { 8, 4, 6, 14, 13, 9, 13 };
  194.         if (offset >= 1 && offset <= 7)
  195.             return testvals[offset - 1];
  196.     }
  197.  
  198.     /* by default, return what was stored there */
  199.     return mappy_customio_2[offset];
  200. }
  201.  
  202.  
  203. /*************************************************************************************
  204.  *
  205.  *         Dig Dug 2 custom I/O ports
  206.  *
  207.  *************************************************************************************/
  208.  
  209. READ_HANDLER( digdug2_customio_1_r )
  210. {
  211.     static int crednum[] = { 1, 1, 2, 2 };
  212.     static int credden[] = { 1, 2, 1, 3 };
  213.     int val, temp, mode = mappy_customio_1[8];
  214.  
  215.     /*logerror("I/O read 1: mode %d offset %d\n", mode, offset);*/
  216.  
  217.     if (io_chip_1_enabled)
  218.     {
  219.         /* mode 3 is the standard, and returns actual important values */
  220.         if (mode == 1 || mode == 3)
  221.         {
  222.             switch (offset)
  223.             {
  224.                 case 0:        /* Coin slots, low nibble of port 4 */
  225.                 {
  226.                     static int lastval;
  227.  
  228.                     val = readinputport (4) & 0x0f;
  229.  
  230.                     /* bit 0 is a trigger for the coin slot */
  231.                     if ((val & 1) && ((val ^ lastval) & 1)) ++credits;
  232.  
  233.                     return lastval = val;
  234.                 }
  235.  
  236.                 case 1:        /* Start buttons, high nibble of port 4 */
  237.                 {
  238.                     static int lastval;
  239.  
  240.                     temp = (readinputport (0) >> 6) & 3;
  241.                     val = readinputport (4) >> 4;
  242.  
  243.                     /* bit 0 is a trigger for the 1 player start */
  244.                     if ((val & 1) && ((val ^ lastval) & 1))
  245.                         if (credits >= credden[temp]) credits -= credden[temp];
  246.                     /* bit 1 is a trigger for the 2 player start */
  247.                     if ((val & 2) && ((val ^ lastval) & 2))
  248.                         if (credits >= 2 * credden[temp]) credits -= 2 * credden[temp];
  249.  
  250.                     return lastval = val;
  251.                 }
  252.  
  253.                 case 2:        /* High BCD of credits */
  254.                     temp = (readinputport (0) >> 6) & 3;
  255.                     return (credits * crednum[temp] / credden[temp]) / 10;
  256.  
  257.                 case 3:        /* Low BCD of credits */
  258.                     temp = (readinputport (0) >> 6) & 3;
  259.                     return (credits * crednum[temp] / credden[temp]) % 10;
  260.  
  261.                 case 4:        /* Player 1 joystick */
  262.                     return readinputport (3) & 0x0f;
  263.  
  264.                 case 5:        /* Player 1 buttons */
  265.                     return readinputport (3) >> 4;
  266.  
  267.                 case 6:        /* Player 2 joystick */
  268.                     return readinputport (5) & 0x0f;
  269.  
  270.                 case 7:        /* Player 2 buttons */
  271.                     return readinputport (5) >> 4;
  272.             }
  273.         }
  274.     }
  275.     /* by default, return what was stored there */
  276.     return mappy_customio_1[offset];
  277. }
  278.  
  279.  
  280. READ_HANDLER( digdug2_customio_2_r )
  281. {
  282.     int mode = mappy_customio_2[8];
  283.  
  284.     /*logerror("I/O read 2: mode %d, offset %d\n", mode, offset);*/
  285.  
  286.     if (io_chip_2_enabled)
  287.     {
  288.         /* mode 4 is the standard, and returns actual important values */
  289.         if (mode == 4)
  290.         {
  291.             switch (offset)
  292.             {
  293.                 case 2:        /* DSW0, low nibble */
  294.                     return readinputport (0) & 0x0f;
  295.  
  296.                 case 4:        /* DSW0, high nibble */
  297.                     return readinputport (0) >> 4;
  298.  
  299.                 case 5:        /* DSW1, high nibble */
  300.                     return readinputport (1) >> 4;
  301.  
  302.                 case 6:        /* DSW1, low nibble */
  303.                     return readinputport (1) & 0x0f;
  304.  
  305.                 case 7:        /* DSW2 - service switch */
  306.                     return readinputport (2) & 0x0f;
  307.  
  308.                 case 0:        /* read, but unknown */
  309.                 case 1:        /* read, but unknown */
  310.                 case 3:        /* read, but unknown */
  311.                     return 0;
  312.             }
  313.         }
  314.     }
  315.     /* by default, return what was stored there */
  316.     return mappy_customio_2[offset];
  317. }
  318.  
  319.  
  320. /*************************************************************************************
  321.  *
  322.  *         Motos custom I/O ports
  323.  *
  324.  *************************************************************************************/
  325.  
  326. READ_HANDLER( motos_customio_1_r )
  327. {
  328.     int val, mode = mappy_customio_1[8];
  329.  
  330.     logerror("I/O read 1: mode %d offset %d\n", mode, offset);
  331.  
  332.     /* mode 1 is the standard, and returns actual important values */
  333.     if (mode == 1)
  334.     {
  335.         switch (offset)
  336.         {
  337.             case 0:        /* Coin slots, low nibble of port 3 */
  338.             {
  339.                 static int lastval;
  340.  
  341.                 val = readinputport (3) & 0x0f;
  342.  
  343.                 /* bit 0 is a trigger for the coin slot */
  344.                 if ((val & 1) && ((val ^ lastval) & 1)) ++credits;
  345.  
  346.                 return lastval = val;
  347.             }
  348.  
  349.             case 1:        /* Player 1 joystick */
  350.                 return readinputport (2) & 0x0f;
  351.             case 3:        /* Start buttons, high nibble of port 3 */
  352.                 return readinputport (3) >> 4;
  353.             case 9:
  354.                 return 0;
  355.             case 2:
  356.             case 4:
  357.             case 5:
  358.             case 6:
  359.             case 7:        /* Player 2 joystick */
  360.                 return readinputport (4) & 0x0f;
  361.         }
  362.     }
  363.     else if (mode == 8)  /* I/O tests chip 1 */
  364.     {
  365.         switch (offset)
  366.         {
  367.             case 0:
  368.                 return 0x06;
  369.                 break;
  370.             case 1:
  371.                 return 0x09;
  372.                 break;
  373.             default:
  374.                 /* by default, return what was stored there */
  375.                 return mappy_customio_2[offset];
  376.         }
  377.     }
  378.  
  379.     /* by default, return what was stored there */
  380.     return mappy_customio_1[offset];
  381. }
  382.  
  383.  
  384. READ_HANDLER( motos_customio_2_r )
  385. {
  386.     int mode = mappy_customio_2[8];
  387.  
  388.     logerror("I/O read 2: mode %d, offset %d\n", mode, offset);
  389.  
  390.     /* mode 9 is the standard, and returns actual important values */
  391.     if (mode == 9)
  392.     {
  393.         switch (offset)
  394.         {
  395.             case 2:        /* DSW0, low nibble */
  396.                 return readinputport (0) & 0x0f;
  397.  
  398.             case 4:        /* DSW0, high nibble */
  399.                 return readinputport (0) >> 4;
  400.  
  401.                   case 6:         /* DSW1, high nibble + Player 1 buttons, high nibble + Player 2? button, high nibble */
  402.  
  403.                         return (readinputport (1) >> 4) | (readinputport (2) >> 4) | (readinputport (4) >> 4);
  404.             case 0:
  405.             case 1:
  406.             case 3:
  407.             case 5:
  408.             case 7:
  409.                 return 0;
  410.         }
  411.     }
  412.     else if (mode == 8)  /* I/O tests chip 2 */
  413.     {
  414.         switch (offset)
  415.         {
  416.             case 0:
  417.                 return 0x06;
  418.                 break;
  419.             case 1:
  420.                 return 0x09;
  421.                 break;
  422.             default:
  423.                 /* by default, return what was stored there */
  424.                 return mappy_customio_2[offset];
  425.         }
  426.     }
  427.  
  428.     /* by default, return what was stored there */
  429.     return mappy_customio_2[offset];
  430. }
  431.  
  432.  
  433. /*************************************************************************************
  434.  *
  435.  *         Tower of Druaga custom I/O ports
  436.  *
  437.  *************************************************************************************/
  438.  
  439. READ_HANDLER( todruaga_customio_1_r )
  440. {
  441.     static int crednum[] = { 1, 1, 2, 2 };
  442.     static int credden[] = { 1, 2, 1, 3 };
  443.     int val, temp, mode = mappy_customio_1[8];
  444.  
  445.     logerror("%04x: I/O read 1: mode %d offset %d\n", cpu_get_pc(), mode, offset);
  446.  
  447.     if (io_chip_1_enabled)
  448.     {
  449.         /* mode 3 is the standard, and returns actual important values */
  450.         if (mode == 1 || mode == 3)
  451.         {
  452.             switch (offset)
  453.             {
  454.                 case 0:        /* Coin slots, low nibble of port 5 */
  455.                 {
  456.                     static int lastval;
  457.  
  458.                     val = readinputport (5) & 0x0f;
  459.  
  460.                     /* bit 0 is a trigger for the coin slot */
  461.                     if ((val & 1) && ((val ^ lastval) & 1)) ++credits;
  462.  
  463.                     return lastval = val;
  464.                 }
  465.  
  466.                 case 1:        /* Start buttons, high nibble of port 5 */
  467.                 {
  468.                     static int lastval;
  469.  
  470.                     temp = (readinputport (0) >> 6) & 3;
  471.                     val = readinputport (5) >> 4;
  472.                     val |= (readinputport (3) & 0x80) >> 7;    /* player 1 start */
  473.  
  474.                     /* bit 0 is a trigger for the 1 player start */
  475.                     if ((val & 1) && ((val ^ lastval) & 1))
  476.                         if (credits >= credden[temp]) credits -= credden[temp];
  477.                     /* bit 1 is a trigger for the 2 player start */
  478.                     if ((val & 2) && ((val ^ lastval) & 2))
  479.                         if (credits >= 2 * credden[temp]) credits -= 2 * credden[temp];
  480.  
  481.                     return lastval = val;
  482.                 }
  483.  
  484.                 case 2:        /* High BCD of credits */
  485.                     temp = (readinputport (0) >> 6) & 3;
  486.                     return (credits * crednum[temp] / credden[temp]) / 10;
  487.  
  488.                 case 3:        /* Low BCD of credits */
  489.                     temp = (readinputport (0) >> 6) & 3;
  490.                     return (credits * crednum[temp] / credden[temp]) % 10;
  491.  
  492.                 case 4:        /* Player 1 joystick */
  493.                     return readinputport (3) & 0x0f;
  494.  
  495.                 case 5:        /* Player 1 buttons */
  496.                     return readinputport (3) >> 4;
  497.  
  498.                 case 6:        /* Player 2 joystick */
  499.                     return readinputport (6) & 0x0f;
  500.  
  501.                 case 7:        /* Player 2 buttons */
  502.                     return readinputport (6) >> 4;
  503.             }
  504.         }
  505.     }
  506.     /* by default, return what was stored there */
  507.     return mappy_customio_1[offset];
  508. }
  509.  
  510.  
  511. READ_HANDLER( todruaga_customio_2_r )
  512. {
  513.     int mode = mappy_customio_2[8];
  514.  
  515.     logerror("%04x: I/O read 2: mode %d, offset %d\n", cpu_get_pc(), mode, offset);
  516.  
  517.     if (io_chip_1_enabled)
  518.     {
  519.         /* mode 4 is the standard, and returns actual important values */
  520.         if (mode == 4)
  521.         {
  522.             switch (offset)
  523.             {
  524.                 case 2:        /* DSW0, low nibble */
  525.                     return readinputport (0) & 0x0f;
  526.  
  527.                 case 4:        /* DSW0, high nibble */
  528.                     return readinputport (0) >> 4;
  529.  
  530.                 case 5:        /* DSW1, high nibble */
  531.                     return readinputport (1) >> 4;
  532.  
  533.                 case 6:        /* DSW1, low nibble */
  534.                     return readinputport (1) & 0x0f;
  535.  
  536.                 case 7:        /* DSW2 - service switch */
  537.                     return readinputport (2) & 0x0f;
  538.  
  539.                 case 0:        /* read, but unknown */
  540.                 case 1:        /* read, but unknown */
  541.                 case 3:        /* read, but unknown */
  542.                     return 0;
  543.             }
  544.         }
  545.     }
  546.  
  547.     /* by default, return what was stored there */
  548.     return mappy_customio_2[offset];
  549. }
  550.  
  551.  
  552.  
  553. WRITE_HANDLER( mappy_interrupt_enable_1_w )
  554. {
  555.     interrupt_enable_1 = offset;
  556. }
  557.  
  558.  
  559.  
  560. int mappy_interrupt_1(void)
  561. {
  562.     if (interrupt_enable_1) return interrupt();
  563.     else return ignore_interrupt();
  564. }
  565.  
  566.  
  567.  
  568. WRITE_HANDLER( mappy_interrupt_enable_2_w )
  569. {
  570.     interrupt_enable_2 = offset;
  571. }
  572.  
  573.  
  574.  
  575. int mappy_interrupt_2(void)
  576. {
  577.     if (interrupt_enable_2) return interrupt();
  578.     else return ignore_interrupt();
  579. }
  580.  
  581.  
  582. WRITE_HANDLER( mappy_cpu_enable_w )
  583. {
  584.     cpu_set_halt_line(1, offset ? CLEAR_LINE : ASSERT_LINE);
  585. }
  586.